﻿package com.amcharts.chartClasses
{
    import com.amcharts.events.*;
    import com.amcharts.geom.*;
    import com.amcharts.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import mx.collections.*;
    import mx.core.*;
    import mx.effects.easing.*;
    import mx.styles.*;

    public class SimpleChartScrollbar extends UIComponent
    {
        private var _grabAndDrag:Class;
        protected var _rotate:Boolean;
        protected var _position:Number;
        private var _resizeArrowVertical:Class;
        protected var _dragRect:Rectangle;
        protected var _dataProvider:ArrayCollection;
        private var _interval:Number;
        private var _grab:Class;
        protected var _hideResizeGrips:Boolean;
        protected var _dragging:Boolean;
        private var _dragIcon:Class;
        protected var _cursorsChanged:Boolean;
        private var _resizeArrowHorizontal:Class;
        protected var _resizingRight:Boolean;
        private var _finalX:Number;
        protected var _mask:Rect;
        protected var _dragIconRight:Sprite;
        protected var _dragIconLeft:Sprite;
        protected var _maskedArea:UIComponent;
        protected var _dispatchEvent:Boolean;
        protected var _dragger:ExtendedSprite;
        private var _bg:Rect;
        private var _selectedBg:Rect;
        protected var _mouseIsOver:Boolean;
        protected var _updateOnReleaseOnly:Boolean;
        protected var _multiplyer:Number;
        private var _tween:Tween;
        protected var _resizingLeft:Boolean;
        private static var stylesInited:Boolean = initStyles();
        static const DEFAULT_MEASURED_HEIGHT:Number = 30;
        static const VERSION:String = "1.8.3.1";

        public function SimpleChartScrollbar() : void
        {
            _dragIcon = SimpleChartScrollbar__dragIcon;
            _resizeArrowHorizontal = SimpleChartScrollbar__resizeArrowHorizontal;
            _resizeArrowVertical = SimpleChartScrollbar__resizeArrowVertical;
            _grab = SimpleChartScrollbar__grab;
            _grabAndDrag = SimpleChartScrollbar__grabAndDrag;
            this.includeInLayout = false;
            addEventListener(Event.ADDED_TO_STAGE, onAdded);
            addEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
            return;
        }// end function

        protected function updateScrollbarSize(param1:Number, param2:Number) : void
        {
            if (_rotate)
            {
                if (param1 < 0)
                {
                    param1 = 0;
                }
                if (param2 > this.height)
                {
                    param2 = this.height;
                }
                _mask.y = param1;
                _mask.height = param2 - param1;
                _mask.width = this.width;
                _dragRect.width = 0;
                _dragRect.height = this.height - _mask.height;
                _dragIconLeft.x = this.width / 2;
                _dragIconRight.x = this.width / 2;
                _dragIconRight.rotation = 90;
                _dragIconLeft.rotation = 90;
            }
            else
            {
                if (param1 < 0)
                {
                    param1 = 0;
                }
                if (param2 > this.width)
                {
                    param2 = this.width;
                }
                _mask.x = param1;
                _mask.width = param2 - param1;
                _mask.height = this.height;
                _dragRect.height = 0;
                _dragRect.width = this.width - _mask.width;
                _dragIconLeft.y = this.height / 2;
                _dragIconRight.y = this.height / 2;
            }
            _dragger.x = _mask.x;
            _dragger.y = _mask.y;
            _dragger.width = _mask.width;
            _dragger.height = _mask.height;
            updateDragIconPositions();
            return;
        }// end function

        override protected function updateDisplayList(param1:Number, param2:Number) : void
        {
            super.updateDisplayList(param1, param2);
            _hideResizeGrips = this.getStyle("hideResizeGrips");
            if (this.height)
            {
            }
            if (this.width)
            {
                if (_hideResizeGrips)
                {
                    _dragIconLeft.visible = false;
                    _dragIconRight.visible = false;
                }
                if (_dataProvider.length > 0)
                {
                    createScrollbar();
                    this.visible = true;
                }
                else
                {
                    this.visible = false;
                }
            }
            return;
        }// end function

        protected function createScrollbar() : void
        {
            _bg.width = this.width;
            _bg.height = this.height;
            Utils.setRGB(_bg, getStyle("backgroundColor"));
            _bg.alpha = getStyle("backgroundAlpha");
            _selectedBg.width = this.width;
            _selectedBg.height = this.height;
            Utils.setRGB(_selectedBg, getStyle("selectedBackgroundColor"));
            _selectedBg.alpha = getStyle("selectedBackgroundAlpha");
            return;
        }// end function

        protected function handleLeftIconDragStart(event:MouseEvent) : void
        {
            stopTween();
            _resizingLeft = true;
            return;
        }// end function

        public function clean() : void
        {
            if (stage)
            {
                stage.removeEventListener(MouseEvent.MOUSE_UP, handleReleaseOutside);
            }
            stopTween();
            clearInterval(_interval);
            return;
        }// end function

        public function destroy() : void
        {
            clean();
            Utils.removeChildren(this);
            return;
        }// end function

        protected function onRemoved(event:Event) : void
        {
            clean();
            return;
        }// end function

        override protected function createChildren() : void
        {
            super.createChildren();
            this.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);
            this.addEventListener(MouseEvent.MOUSE_OUT, handleMouseOut);
            _bg = new Rect(10, 10, [0], [1]);
            addChildAt(_bg, 0);
            _bg.addEventListener(MouseEvent.MOUSE_UP, handleBackgroundClick);
            _maskedArea = new UIComponent();
            addChild(_maskedArea);
            _selectedBg = new Rect(10, 10, [0], [1]);
            _selectedBg.mouseEnabled = false;
            _selectedBg.mouseChildren = false;
            _maskedArea.addChildAt(_selectedBg, 0);
            _mask = new Rect(10, 10, [0], [1]);
            addChild(_mask);
            _maskedArea.mask = _mask;
            _dragger = new ExtendedSprite();
            addChild(_dragger);
            var _loc_1:* = new Rect(10, 10, [0], [0]);
            _dragger.addChild(_loc_1);
            _dragger.addEventListener(MouseEvent.MOUSE_DOWN, handleDragStart);
            _dragger.addEventListener(MouseEvent.MOUSE_UP, handleDragStop);
            _dragger.addEventListener(MouseEvent.MOUSE_OVER, handleDraggerOver);
            _dragRect = new Rectangle(0, 0, 10, 10);
            _dragIconLeft = addChild(new _dragIcon()) as Sprite;
            _dragIconRight = addChild(new _dragIcon()) as Sprite;
            _dragIconRight.addEventListener(MouseEvent.MOUSE_DOWN, handleRightIconDragStart);
            _dragIconRight.addEventListener(MouseEvent.MOUSE_UP, handleRightIconDragStop);
            _dragIconRight.addEventListener(MouseEvent.MOUSE_OVER, handleIconRollOver);
            _dragIconLeft.addEventListener(MouseEvent.MOUSE_DOWN, handleLeftIconDragStart);
            _dragIconLeft.addEventListener(MouseEvent.MOUSE_UP, handleLeftIconDragStop);
            _dragIconLeft.addEventListener(MouseEvent.MOUSE_OVER, handleIconRollOver);
            this.visible = false;
            return;
        }// end function

        protected function handleDragStop(event:MouseEvent) : void
        {
            if (_updateOnReleaseOnly)
            {
                updateScrollbar();
                dispatchScrollbarEvent();
            }
            _dragging = false;
            _dragger.stopDrag();
            if (_mouseIsOver)
            {
                removeCursors();
                this.cursorManager.setCursor(_grab);
                _cursorsChanged = true;
            }
            updateScrollbar();
            return;
        }// end function

        public function stopTween() : void
        {
            if (_tween)
            {
                _tween.stop();
                _tween = null;
            }
            return;
        }// end function

        protected function handleRightIconDragStart(event:MouseEvent) : void
        {
            stopTween();
            _resizingRight = true;
            return;
        }// end function

        protected function handleMouseOut(event:MouseEvent) : void
        {
            _mouseIsOver = false;
            hideDragIcons();
            return;
        }// end function

        public function get updateOnReleaseOnly() : Boolean
        {
            return _updateOnReleaseOnly;
        }// end function

        protected function removeCursors() : void
        {
            if (_cursorsChanged)
            {
                this.cursorManager.removeAllCursors();
                _cursorsChanged = false;
            }
            return;
        }// end function

        protected function hideDragIcons() : void
        {
            if (!_resizingLeft)
            {
            }
            if (!_resizingRight)
            {
            }
            if (!_dragging)
            {
                if (_hideResizeGrips)
                {
                    _dragIconLeft.visible = false;
                    _dragIconRight.visible = false;
                }
                removeCursors();
            }
            return;
        }// end function

        public function relativeZoom(param1:Number, param2:Number) : void
        {
            var _loc_4:Number = NaN;
            _multiplyer = param1;
            _position = param2;
            var _loc_3:* = param2;
            if (_rotate)
            {
                _loc_4 = _loc_3 + this.height / param1;
            }
            else
            {
                _loc_4 = _loc_3 + this.width / param1;
            }
            updateScrollbarSize(_loc_3, _loc_4);
            return;
        }// end function

        public function set rotate(param1:Boolean) : void
        {
            _rotate = param1;
            if (_rotate)
            {
                if (!this.width)
                {
                    this.width = DEFAULT_MEASURED_HEIGHT;
                }
            }
            else if (!this.height)
            {
                this.height = DEFAULT_MEASURED_HEIGHT;
            }
            return;
        }// end function

        protected function dispatchScrollbarEvent() : void
        {
            var _loc_5:Number = NaN;
            var _loc_6:Number = NaN;
            var _loc_7:Number = NaN;
            var _loc_1:* = _dragger.x;
            var _loc_2:* = _dragger.y;
            var _loc_3:* = _dragger.width;
            var _loc_4:* = _dragger.height;
            if (_rotate)
            {
                _loc_5 = _loc_2;
                _loc_6 = _loc_4;
                _loc_7 = this.height / _loc_4;
            }
            else
            {
                _loc_5 = _loc_1;
                _loc_6 = _loc_3;
                _loc_7 = this.width / _loc_3;
            }
            var _loc_8:* = new ScrollbarEvent(ScrollbarEvent.ZOOMED);
            _loc_8.position = _loc_5;
            _loc_8.multiplyer = _loc_7;
            dispatchEvent(_loc_8);
            return;
        }// end function

        protected function showDragIcons() : void
        {
            if (getStyle("resizeEnabled"))
            {
                _dragIconLeft.visible = true;
                _dragIconRight.visible = true;
            }
            return;
        }// end function

        protected function handleBackgroundClick(event:MouseEvent) : void
        {
            var _loc_2:String = null;
            var _loc_3:Number = NaN;
            var _loc_4:Number = NaN;
            var _loc_5:Number = NaN;
            if (!_resizingRight)
            {
            }
            if (!_resizingLeft)
            {
                _loc_5 = getStyle("scrollDuration");
                if (_rotate)
                {
                    _loc_2 = "y";
                    _loc_3 = _dragger.y;
                    _loc_4 = this.mouseY - _dragger.height / 2;
                    _loc_4 = Utils.fitToBounds(_loc_4, 0, this.height - _dragger.height);
                }
                else
                {
                    _loc_2 = "x";
                    _loc_3 = _dragger.x;
                    _loc_4 = this.mouseX - _dragger.width / 2;
                    _loc_4 = Utils.fitToBounds(_loc_4, 0, this.width - _dragger.width);
                }
                if (_updateOnReleaseOnly)
                {
                    _dragger[_loc_2] = _loc_4;
                    _dispatchEvent = true;
                    dispatchScrollbarEvent();
                }
                else
                {
                    stopTween();
                    _tween = new Tween(_dragger, _loc_2, Exponential.easeOut, _loc_3, _loc_4, _loc_5);
                }
            }
            return;
        }// end function

        protected function handleDraggerOver(event:MouseEvent) : void
        {
            if (!_resizingLeft)
            {
            }
            if (!_resizingRight)
            {
                this.cursorManager.setCursor(_grab);
                _cursorsChanged = true;
            }
            return;
        }// end function

        protected function handleDragStart(event:MouseEvent) : void
        {
            stopTween();
            removeCursors();
            this.cursorManager.setCursor(_grabAndDrag);
            _cursorsChanged = true;
            _dragging = true;
            _dragger.startDrag(false, _dragRect);
            return;
        }// end function

        protected function handleMouseOver(event:MouseEvent) : void
        {
            _mouseIsOver = true;
            showDragIcons();
            return;
        }// end function

        protected function handleIconRollOver(event:MouseEvent) : void
        {
            if (_rotate)
            {
                this.cursorManager.setCursor(_resizeArrowVertical);
            }
            else
            {
                this.cursorManager.setCursor(_resizeArrowHorizontal);
            }
            _cursorsChanged = true;
            return;
        }// end function

        protected function updateDragIconPositions() : void
        {
            var _loc_1:* = _dragger.x;
            var _loc_2:* = _dragger.y;
            if (_rotate)
            {
                _dragIconLeft.y = _loc_2;
                _dragIconRight.y = _loc_2 + _dragger.height;
            }
            else
            {
                _dragIconLeft.x = _loc_1;
                _dragIconRight.x = _loc_1 + _dragger.width;
            }
            return;
        }// end function

        protected function updateScrollbar() : void
        {
            var _loc_1:Number = NaN;
            var _loc_3:Number = NaN;
            var _loc_4:Number = NaN;
            var _loc_5:Number = NaN;
            var _loc_6:Number = NaN;
            var _loc_7:Number = NaN;
            var _loc_8:Number = NaN;
            var _loc_2:Boolean = false;
            if (_resizingRight)
            {
                if (_rotate)
                {
                    _loc_1 = this.mouseY - _dragger.y;
                    if (_loc_1 + _dragger.y > this.height)
                    {
                        _loc_1 = this.height - _dragger.y;
                    }
                    if (_loc_1 < 0)
                    {
                        _resizingRight = false;
                        _resizingLeft = true;
                        _loc_2 = true;
                    }
                    else
                    {
                        _dragger.height = _loc_1;
                    }
                }
                else
                {
                    _loc_1 = this.mouseX - _dragger.x;
                    if (_loc_1 + _dragger.x > this.width)
                    {
                        _loc_1 = this.width - _dragger.x;
                    }
                    if (_loc_1 < 0)
                    {
                        _resizingRight = false;
                        _resizingLeft = true;
                        _loc_2 = true;
                    }
                    else
                    {
                        _dragger.width = _loc_1;
                    }
                }
            }
            if (_resizingLeft)
            {
                if (_rotate)
                {
                    _loc_3 = _dragger.y;
                    _loc_4 = this.mouseY;
                    if (_loc_4 < 0)
                    {
                        _loc_4 = 0;
                    }
                    if (_loc_4 > this.height)
                    {
                        _loc_4 = this.height;
                    }
                    if (_loc_2 == true)
                    {
                        _loc_1 = _loc_3 - _loc_4;
                    }
                    else
                    {
                        _loc_1 = _dragger.height + _loc_3 - _loc_4;
                    }
                    if (_loc_1 < 0)
                    {
                        _resizingRight = true;
                        _resizingLeft = false;
                        _dragger.y = _loc_3 + _dragger.height;
                    }
                    else
                    {
                        _dragger.y = _loc_4;
                        _dragger.height = _loc_1;
                    }
                }
                else
                {
                    _loc_3 = _dragger.x;
                    _loc_4 = this.mouseX;
                    if (_loc_4 < 0)
                    {
                        _loc_4 = 0;
                    }
                    if (_loc_4 > this.width)
                    {
                        _loc_4 = this.width;
                    }
                    if (_loc_2 == true)
                    {
                        _loc_1 = _loc_3 - _loc_4;
                    }
                    else
                    {
                        _loc_1 = _dragger.width + _loc_3 - _loc_4;
                    }
                    if (_loc_1 < 0)
                    {
                        _resizingRight = true;
                        _resizingLeft = false;
                        _dragger.x = _loc_3 + _dragger.width;
                    }
                    else
                    {
                        _dragger.x = _loc_4;
                        _dragger.width = _loc_1;
                    }
                }
            }
            if (_mask.x == _dragger.x)
            {
            }
            if (_mask.y == _dragger.y)
            {
            }
            if (_mask.width == _dragger.width)
            {
            }
            if (_mask.height != _dragger.height)
            {
                _loc_5 = _dragger.x;
                _loc_6 = _dragger.y;
                _loc_7 = _dragger.width;
                _loc_8 = _dragger.height;
                _mask.x = _loc_5;
                _mask.y = _loc_6;
                _mask.width = _loc_7;
                _mask.height = _loc_8;
                if (_rotate)
                {
                    _dragRect.height = this.height - _loc_8;
                }
                else
                {
                    _dragRect.width = this.width - _loc_7;
                }
                updateDragIconPositions();
                if (!_updateOnReleaseOnly)
                {
                    dispatchScrollbarEvent();
                }
            }
            return;
        }// end function

        protected function handleLeftIconDragStop(event:MouseEvent) : void
        {
            _resizingLeft = false;
            if (_updateOnReleaseOnly)
            {
                updateScrollbar();
                dispatchScrollbarEvent();
            }
            if (!_mouseIsOver)
            {
                removeCursors();
            }
            return;
        }// end function

        protected function handleRightIconDragStop(event:MouseEvent) : void
        {
            if (_updateOnReleaseOnly)
            {
                updateScrollbar();
                dispatchScrollbarEvent();
            }
            _resizingRight = false;
            if (!_mouseIsOver)
            {
                removeCursors();
            }
            return;
        }// end function

        public function set updateOnReleaseOnly(param1:Boolean) : void
        {
            _updateOnReleaseOnly = param1;
            return;
        }// end function

        protected function onAdded(event:Event) : void
        {
            _interval = setInterval(updateScrollbar, 20);
            stage.removeEventListener(MouseEvent.MOUSE_UP, handleReleaseOutside);
            stage.addEventListener(MouseEvent.MOUSE_UP, handleReleaseOutside);
            return;
        }// end function

        protected function handleReleaseOutside(event:MouseEvent) : void
        {
            if (!_resizingLeft)
            {
            }
            if (!_resizingRight)
            {
            }
            if (_dragging)
            {
                if (_updateOnReleaseOnly)
                {
                    updateScrollbar();
                    dispatchScrollbarEvent();
                }
            }
            _resizingLeft = false;
            _resizingRight = false;
            _dragging = false;
            _dragger.stopDrag();
            if (!_mouseIsOver)
            {
                removeCursors();
                if (_hideResizeGrips)
                {
                    _dragIconLeft.visible = false;
                    _dragIconRight.visible = false;
                }
                updateScrollbar();
            }
            return;
        }// end function

        public function set dataProvider(param1:ArrayCollection) : void
        {
            _dataProvider = param1;
            invalidateDisplayList();
            return;
        }// end function

        private static function initStyles() : Boolean
        {
            var styleDeclaration:* = StyleManager.getStyleDeclaration("SimpleChartScrollbar");
            if (!styleDeclaration)
            {
                styleDeclaration = new CSSStyleDeclaration();
            }
            styleDeclaration.defaultFactory = function () : void
            {
                this.backgroundColor = 13948116;
                this.backgroundAlpha = 1;
                this.selectedBackgroundColor = 15724527;
                this.selectedBackgroundAlpha = 1;
                this.scrollDuration = 2;
                this.resizeEnabled = true;
                this.hideResizeGrips = true;
                return;
            }// end function
            ;
            StyleManager.setStyleDeclaration("SimpleChartScrollbar", styleDeclaration, false);
            return true;
        }// end function

    }
}
